
#line 1 "..\..\main.c" /0
 
 
 
 
 
 
#error *** WARNING C318 IN LINE 6 OF ..\..\main.c: can't open file 'stc15.h'
  
#line 1 "D:\2-Embed\2.1-Keil_v5\C51\INC\stdio.h" /0






 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 typedef unsigned int size_t;
 
 
 #pragma SAVE
 #pragma REGPARMS
 extern char _getkey (void);
 extern char getchar (void);
 extern char ungetchar (char);
 extern char putchar (char);
 extern int printf   (const char *, ...);
 extern int sprintf  (char *, const char *, ...);
 extern int vprintf  (const char *, char *);
 extern int vsprintf (char *, const char *, char *);
 extern char *gets (char *, int n);
 extern int scanf (const char *, ...);
 extern int sscanf (char *, const char *, ...);
 extern int puts (const char *);
 
 #pragma RESTORE
 
 
 
#line 7 "..\..\main.c" /0
 
 
 
#error *** WARNING C318 IN LINE 9 OF ..\..\main.c: can't open file 'key.h'


 
 
 
 
 enum PERIPHERAL
 {
 LED = 0x80,
 DEVICE = 0xa0,
 BIT = 0xc0,
 SEG = 0xe0
 };
 
 


 
 
 
 bit LED0, LED1, LED2, LED3, LED4, LED5, LED6, LED7;
 
 bit Buzzer;
 
 bit Relay;
 
 
 uchar Dsp_Bit[16] = {0};
 
 uchar Dsp_Code[8] = {0};
 
 uint	Tick;
 
 
 
 uchar	State;
 uchar 	Key_Value;
 float 	Calc_Num1,Calc_Num2;
 float	Result;
 uchar	Operator;
 uchar	Button_Flag;
 
 float	Test = 12.5;
 
 





 
 void Timer2_Init(void)		 
 {
 AUXR |= 0x04;		 
 T2L = 0x20;			 
 T2H = 0xD1;			 
 AUXR |= 0x10;		 
 
 IE2 |= 0x04;		 
 EA = 1;
 }
 





 
 void Timer2_Service() interrupt 12
 {
 static uchar dsp_com;
 
 
 Tick++;		
 
 
  P0=~((uchar)LED0|(uchar)LED1<<1|(uchar)LED2<<2|(uchar)LED3<<3|(uchar)LED4<<4|(uchar)LED5<<5|(uchar)LED6<<6|(uchar)LED7<<7);P2=LED;P2=0;
 
  P0=(uchar)Buzzer<<6|(uchar)Relay<<4;P2=DEVICE;P2=0;	
 
  P0=0;P2=BIT;P2=0;									 
  P0=Dsp_Code[dsp_com];P2=SEG;P2=0;		 
  P0=1 << (dsp_com);P2=BIT;P2=0;				 
 if(++dsp_com > 7)								 
 dsp_com = 0;
 }
 
 





 
 void Seg_Tran(void)
 {
 unsigned char i, j=0, temp;
 for (i=0; i<8; i++, j++)
 {
 switch (Dsp_Bit[j]) 
 {  
 case '0': temp = 0xc0; break;
 case '1': temp = 0xf9; break;
 
 case '2': temp = 0xa4; break;
 case '3': temp = 0xb0; break;
 
 case '4': temp = 0x99; break;
 
 case '5': temp = 0x92; break;
 case '6': temp = 0x82; break;
 
 case '7': temp = 0xf8; break;
 
 case '8': temp = 0x80; break;
 case '9': temp = 0x90; break;
 
 case 'A': temp = 0x88; break;
 case 'B': temp = 0x83; break;
 case 'C': temp = 0xc6; break;
 case 'D': temp = 0xA1; break;
 case 'E': temp = 0x86; break;
 case 'e': temp = 0x86; break;
 case 'F': temp = 0x8E; break;
 case 'H': temp = 0x89; break;
 case 'L': temp = 0xC7; break;
 case 'N': temp = 0xC8; break;
 case 'P': temp = 0x8c; break;
 case 'U': temp = 0xC1; break;
 case '-': temp = 0xbf; break;
 case ' ': temp = 0xff; break;
 case '^': temp = 0xfe; break;  
 case '_': temp = 0xf7; break;  
 default: temp = 0xff;
 }
 if (Dsp_Bit[j+1] == '.')
 {
 temp = temp&0x7f;              
 j++;
 }
 Dsp_Code[i] = temp;
 }
 }
 
 
 
 
 
 
 
 
  
#line 1 "D:\2-Embed\2.1-Keil_v5\C51\INC\math.h" /0







 
 
 
 
 
 
#line 14 "D:\2-Embed\2.1-Keil_v5\C51\INC\math.h" /1
 
  
 
 
 
  
 
 
 
 
 
 
 
 
#line 28 "D:\2-Embed\2.1-Keil_v5\C51\INC\math.h" /0
 
 #pragma SAVE
 #pragma REGPARMS
 
 extern char  cabs  (char  val);
 extern int    abs  (int   val);
 extern long  labs  (long  val);
 
 
 extern float fabs  (float val);
 extern float sqrt  (float val);
 extern float exp   (float val);
 extern float log   (float val);
 extern float log10 (float val);
 extern float sin   (float val);
 extern float cos   (float val);
 extern float tan   (float val);
 extern float asin  (float val);
 extern float acos  (float val);
 extern float atan  (float val);
 extern float sinh  (float val);
 extern float cosh  (float val);
 extern float tanh  (float val);
 extern float atan2 (float y, float x);
 
 extern float ceil  (float val);
 extern float floor (float val);
 extern float modf  (float val, float *n);
 extern float fmod  (float x, float y);
 extern float pow   (float x, float y);
 
 
#line 60 "D:\2-Embed\2.1-Keil_v5\C51\INC\math.h" /1
 
 
 
#line 63 "D:\2-Embed\2.1-Keil_v5\C51\INC\math.h" /0
 
 #pragma RESTORE
 
 
#line 161 "..\..\main.c" /0
 
 
 int Is_integer_float(float num) 
 {
 
 float abs_num = fabs(num);
 
 int int_part = (int)abs_num;
 
 float frac_part = abs_num - int_part;
 
 if (frac_part < 1e-7) {
 return 1;  
 } else {
 return 0;  
 }
 }
 
 
 
 
 
 
 
 
 void Display_Task(void)
 {
 static uchar Display_Tick;
 
 if((Tick - Display_Tick) > 100)
 {	
 Display_Tick = Tick;
 switch (State)
 {
 case 0: 
 if (Is_integer_float(Calc_Num1)) 
 {	 
 sprintf(Dsp_Bit, "%8.7g", (float)Calc_Num1);
 } else {	 
 sprintf(Dsp_Bit, "%9.7g", (float)Calc_Num1);
 }
 LED0 = 1; LED1 = 0; LED2 = 0; LED3 = 0;
 break;
 case 1: 
 if (Is_integer_float(Calc_Num1)) 
 {
 sprintf(Dsp_Bit, "%8.7g", (float)Calc_Num1);
 } else {
 sprintf(Dsp_Bit, "%9.7g", (float)Calc_Num1);
 }
 LED0 = 0; LED1 = 1; LED2 = 0; LED3 = 0;
 
 switch (Operator)
 { 
 case '+':
 LED4 = 1; LED5 = 0; LED6 = 0; LED7 = 0;
 break;
 case '-':
 LED4 = 0; LED5 = 1; LED6 = 0; LED7 = 0;
 break;
 case '*':
 LED4 = 0; LED5 = 0; LED6 = 1; LED7 = 0;
 break;
 case '/':
 LED4 = 0; LED5 = 0; LED6 = 0; LED7 = 1;
 break;
 default:
 LED4 = 0; LED5 = 0; LED6 = 0; LED7 = 0;
 break;
 }
 break;
 case 2: 
 sprintf(Dsp_Bit, "%8.7g", (float)Calc_Num2);
 LED0 = 0; LED1 = 0; LED2 = 1; LED3 = 0;
 break;
 case 3: 
 if (Is_integer_float(Result)) 
 {
 sprintf(Dsp_Bit, "%8.7g", (float)Result);
 } else {
 sprintf(Dsp_Bit, "%9.7g", (float)Result);
 }
 LED0 = 0; LED1 = 0; LED2 = 0; LED3 = 1;
 break;
 default:
 break;
 }
 }
 Seg_Tran();
 }
 
 
 void Calculate(void)
 {
 switch (Operator)
 { 
 case '+':
 Result = Calc_Num1 + Calc_Num2;
 break;
 case '-':
 Result = Calc_Num1 - Calc_Num2;
 break;
 case '*':
 Result = Calc_Num1 * Calc_Num2;
 break;
 case '/':
 Result = Calc_Num1 / Calc_Num2;
 break;
 default:
 break;
 }	
 }
 
 void Calculator_Task(void)
 {
 if(Key_Value != 'N') 
 {
 Button_Flag = 1;
 switch (State)
 {
 case 0: 
 switch (Key_Value)
 {
 
 case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:
 Calc_Num1 = 10*Calc_Num1 + Key_Value;
 break;
 
 case '+': case '-': case '*': case '/': 
 Operator = Key_Value; 
 State = 1;
 break;
 
 case 'C':
 State = 0;
 Calc_Num1 = 0;
 Calc_Num2 = 0;
 Operator = 0;
 break;
 
 default:
 break;
 }
 break;
 case 1: 
 switch (Key_Value)
 {
 
 case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:
 Calc_Num2 = Key_Value;
 State = 2;
 break;
 
 case '+': case '-': case '*': case '/': 
 Operator = Key_Value; 
 break;
 
 case 'C':
 State = 0;
 Calc_Num1 = 0;
 Calc_Num2 = 0;
 Operator = 0;
 break;
 
 default:
 break;
 }				
 break;
 case 2: 
 switch (Key_Value)
 {
 
 case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:
 Calc_Num2 = 10*Calc_Num2 + Key_Value;
 break;
 
 case '+': case '-': case '*': case '/': 
 Calculate();
 State = 1;
 Calc_Num1 = Result; 
 Operator = Key_Value; 
 break;
 
 case 'C':
 State = 0;
 Calc_Num1 = 0;
 Calc_Num2 = 0;
 Operator = 0;
 break;
 
 case '=':
 Calculate();
 State = 3;
 break;
 default:
 break;
 }				
 break;
 case 3: 
 switch (Key_Value)
 {
 
 case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:
 Calc_Num1 = Key_Value;
 State = 0;
 break;
 
 case '+': case '-': case '*': case '/': 
 Calc_Num1 = Result; 
 Operator = Key_Value; 
 State = 1;
 break;
 
 case 'C':
 State = 0;
 Calc_Num1 = 0;
 Calc_Num2 = 0;
 Operator = 0;
 break;
 
 case '=':
 Calc_Num1 = Result; 
 Calculate();
 State = 3;
 break;
 default:
 break;
 }				
 break;
 default:
 break;
 }
 }
 Key_Value = 'N';
 }
 
 
 uchar Key_Tran(uchar keynum)
 {
 uchar keyvalue;
 switch (keynum)
 {
 case 4:		keyvalue = 'C'; break;
 
 case 12:	keyvalue = '='; break;
 
 case 16:	keyvalue = '+' ; break;
 case 17:	keyvalue = '-' ; break;
 case 18:	keyvalue = '*' ; break;
 case 19:	keyvalue = '/' ; break;
 
 case 8:		keyvalue = 0; break;
 case 5:		keyvalue = 1; break;
 case 9:		keyvalue = 2; break;
 case 13:	keyvalue = 3; break;
 case 6:		keyvalue = 4; break;
 case 10:	keyvalue = 5; break;
 case 14:	keyvalue = 6; break;
 case 7:		keyvalue = 7; break;
 case 11:	keyvalue = 8; break;
 case 15:	keyvalue = 9; break;
 
 default:	keyvalue = 'N';	break;
 }
 return keyvalue;
 }	
 





 
 void Key_Task(void)
 {
 static uint Key_Tick;							
 
 
 if((Tick - Key_Tick) > 10)
 {
 Key_Tick = Tick;
 
 Key_Value = Key_Tran(MatKeyScan_4x4());			
 }
 }	
 
 
 





 
 void Data_Task(void)
 {
 static uint Data_Tick;
 
 if((Tick - Data_Tick) > 500)
 {	
 Data_Tick = Tick;
 }
 
 }
 





 
 void Logic_Task(void)
 {
 static uint Button_Tick;
 
 
 if((Tick - Button_Tick) > 100)
 {
 Button_Tick = Tick;
 if(Button_Flag)
 {
 if(Button_Flag == 1)
 {
 Buzzer = 1;
 Button_Flag++;			
 }
 else
 {
 Buzzer = 0;
 Button_Flag = 0;					
 }	
 }
 }
 
 }
 





 
 void P_Init(void)				 
 {
  P0=0xff;P2=LED;P2=0;		 
  P0=0x00;P2=DEVICE;P2=0;	 
  P0=0x00;P2=BIT;P2=0;		 
  P0=0xff;P2=SEG;P2=0;		 
 
 Timer2_Init(); 
 }
 


 
 void main(void)
 {
 
 P_Init();
 
 
 while(1)
 {
 Display_Task();
 Key_Task();
 Calculator_Task();
 Logic_Task();
 }        
 }
